package weaver.interfaces.linearin.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.linearin.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecologyidea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 20231130
 * @Description: 差旅费报销审批（先积）流程归档后调用生成凭证接口
 * @ModifyLog:
 **/
public class WeaClfbxlcBBAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaClfbxlcBBAction>>>>>requestid=" + requestid);
        try {
            JSONObject dataJson = new JSONObject();
            String message = "调用金蝶系统接口返回:";
            String bookid = "003";
            RecordSet rs = new RecordSet();
            ResourceComInfo comInfo = new ResourceComInfo();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sqr = Util.null2String(rs.getString("sqr"));
            String zfrq = Util.null2String(rs.getString("zfrq"));
            String jdrybm = Util.null2String(rs.getString("jdrybm"));
            String jdbmbm = Util.null2String(rs.getString("jdbmbm"));
            double xxje = Util.getDoubleValue(rs.getString("xxje"));
            double bccxje = Util.getDoubleValue(rs.getString("bccxje"));
            String zfzhdxwb = Util.null2String(rs.getString("zfzhdxwb"));
            double yxzfje = Util.getDoubleValue(rs.getString("yxzfje"));
            String lcbh = Util.null2String(rs.getString("lcbh"));
            //单据体：FEntity
            JSONArray FEntity = new JSONArray();
            /*单据体第一行*/
            rs.executeQuery("select yfdl,hjkm,xm,sum(fpjebhs) fpjebhs from " + tablename + "_dt1 " +
                    "where mainid=? group by yfdl,hjkm,xm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String hjkm = Util.null2String(rs.getString("hjkm"));
                String yfdl = Util.null2String(rs.getString("yfdl"));
                double fpjebhs = Util.getDoubleValue(rs.getString("fpjebhs"));
                String xm = Util.null2String(rs.getString("xm"));
                if (fpjebhs > 0) {
                    //摘要
                    String fexplanation = lcbh+comInfo.getLastname(sqr) + "报销差旅费交通费";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", hjkm);
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }
                    //研发大类
                    if (!"".equals(yfdl)) {
                        JSONObject FDETAILID__FF100002 = new JSONObject();
                        FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                        FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                    }
                    //金蝶人员编码
                    if (!"".equals(jdrybm)) {
                        JSONObject FDETAILID__FFLEX7 = new JSONObject();
                        FDETAILID__FFLEX7.put("FNumber", jdrybm);
                        FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    }
                    //金蝶项目编码
                    if (!"".equals(xm)) {
                        JSONObject FDETAILID__FF100019 = new JSONObject();
                        FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                        FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpjebhs);
                    FEntity.add(FEntityJSON);
                }
            }
            /*单据体第二行*/
            rs.executeQuery("select fphm,sum(se) se from " + tablename + "_dt1 " +
                    "where mainid=? and fplx=1 group by fphm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String fphm = Util.null2String(rs.getString("fphm"));
                double se = Util.getDoubleValue(rs.getString("se"));
                if (se > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费交通费" + fphm;
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2221.01.01.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", se);
                    FEntity.add(FEntityJSON);
                }

            }
            /*单据体第三行*/
            rs.executeQuery("select yfdl,hjkm,xm,sum(jehjrmb) jehjrmb from " + tablename + "_dt3 " +
                    "where mainid=? group by yfdl,hjkm,xm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String hjkm = Util.null2String(rs.getString("hjkm"));
                String yfdl = Util.null2String(rs.getString("yfdl"));
                double jehjrmb = Util.getDoubleValue(rs.getString("jehjrmb"));
                String xm = Util.null2String(rs.getString("xm"));
                if (jehjrmb > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他交通费";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", hjkm);
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }
                    //研发大类
                    if (!"".equals(yfdl)) {
                        JSONObject FDETAILID__FF100002 = new JSONObject();
                        FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                        FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                    }
                    //金蝶人员编码
                    if (!"".equals(jdrybm)) {
                        JSONObject FDETAILID__FFLEX7 = new JSONObject();
                        FDETAILID__FFLEX7.put("FNumber", jdrybm);
                        FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    }
                    //金蝶项目编码
                    if (!"".equals(xm)) {
                        JSONObject FDETAILID__FF100019 = new JSONObject();
                        FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                        FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", jehjrmb);
                    FEntity.add(FEntityJSON);
                }
            }
            /*单据体第四行*/
            rs.executeQuery("select yfdl,hjkm,xm,sum(zsfbhsje) zsfbhsje from " + tablename + "_dt2 " +
                    "where mainid=? group by yfdl,hjkm,xm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String hjkm = Util.null2String(rs.getString("hjkm"));
                String yfdl = Util.null2String(rs.getString("yfdl"));
                double zsfbhsje = Util.getDoubleValue(rs.getString("zsfbhsje"));
                String xm = Util.null2String(rs.getString("xm"));
                if (zsfbhsje > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费住宿费";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", hjkm);
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }
                    //研发大类
                    if (!"".equals(yfdl)) {
                        JSONObject FDETAILID__FF100002 = new JSONObject();
                        FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                        FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                    }
                    //金蝶人员编码
                    if (!"".equals(jdrybm)) {
                        JSONObject FDETAILID__FFLEX7 = new JSONObject();
                        FDETAILID__FFLEX7.put("FNumber", jdrybm);
                        FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    }
                    //金蝶项目编码
                    if (!"".equals(xm)) {
                        JSONObject FDETAILID__FF100019 = new JSONObject();
                        FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                        FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", zsfbhsje);
                    FEntity.add(FEntityJSON);
                }
            }
            /*单据体第五行*/
            rs.executeQuery("select fphm,sum(fpse) fpse from " + tablename + "_dt2 " +
                    "where mainid=? and fplx=1 group by fphm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String fphm = Util.null2String(rs.getString("fphm"));
                double fpse = Util.getDoubleValue(rs.getString("fpse"));
                if (fpse > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费住宿费" + fphm;
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2221.01.01.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpse);
                    FEntity.add(FEntityJSON);
                }

            }
            /*单据体第六行*/
            rs.executeQuery("select yfdl,hjkm,xm,sum(fpjebhsje) fpjebhsje from " + tablename + "_dt4 " +
                    "where mainid=? group by yfdl,hjkm,xm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String hjkm = Util.null2String(rs.getString("hjkm"));
                String yfdl = Util.null2String(rs.getString("yfdl"));
                double fpjebhsje = Util.getDoubleValue(rs.getString("fpjebhsje"));
                String xm = Util.null2String(rs.getString("xm"));
                if (fpjebhsje > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他费用";
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", hjkm);
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //核算维度
                    JSONObject FDetailID = new JSONObject();
                    //金蝶部门编码
                    if (!"".equals(jdbmbm)) {
                        JSONObject FDETAILID__FFLEX5 = new JSONObject();
                        FDETAILID__FFLEX5.put("FNumber", jdbmbm);
                        FDetailID.put("FDETAILID__FFLEX5", FDETAILID__FFLEX5);
                    }
                    //研发大类
                    if (!"".equals(yfdl)) {
                        JSONObject FDETAILID__FF100002 = new JSONObject();
                        FDETAILID__FF100002.put("FNumber", KingdeeUtil.getYfdlBm(yfdl));
                        FDetailID.put("FDETAILID__FF100002", FDETAILID__FF100002);
                    }
                    //金蝶人员编码
                    if (!"".equals(jdrybm)) {
                        JSONObject FDETAILID__FFLEX7 = new JSONObject();
                        FDETAILID__FFLEX7.put("FNumber", jdrybm);
                        FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                    }
                    //金蝶项目编码
                    if (!"".equals(xm)) {
                        JSONObject FDETAILID__FF100019 = new JSONObject();
                        FDETAILID__FF100019.put("FNumber", KingdeeUtil.getXmbh(xm));
                        FDetailID.put("FDETAILID__FF100019", FDETAILID__FF100019);
                    }
                    FEntityJSON.put("FDetailID", FDetailID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpjebhsje);
                    FEntity.add(FEntityJSON);
                }
            }
            /*单据体第七行*/
            rs.executeQuery("select fphm,sum(fpse) fpse from " + tablename + "_dt4 " +
                    "where mainid=? and fplx=1 group by fphm", mainid);
            while (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                String fphm = Util.null2String(rs.getString("fphm"));
                double fpse = Util.getDoubleValue(rs.getString("fpse"));
                if (fpse > 0) {
                    //摘要
                    String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费其他费用" + fphm;
                    FEntityJSON.put("FEXPLANATION", fexplanation);
                    //科目编码
                    JSONObject FACCOUNTID = new JSONObject();
                    FACCOUNTID.put("FNumber", "2221.01.01.01");
                    FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                    //借方金额
                    FEntityJSON.put("FDEBIT", fpse);
                    FEntity.add(FEntityJSON);
                }

            }
            /*单据体第八行*/
            boolean flag8 = xxje > 0;
            if (flag8) {
                //摘要
                JSONObject FEntityJSON = new JSONObject();
                String fexplanation = lcbh+""+comInfo.getLastname(sqr) + "报销差旅费差旅费";
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "2241.03");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //金蝶人员编码
                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put("FNumber", jdrybm);
                FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                FEntityJSON.put("FDetailID", FDetailID);
                //贷方金额
                FEntityJSON.put("FCREDIT", xxje);
                FEntity.add(FEntityJSON);
            }
            /*单据体第九行*/
            boolean flag9 = xxje > 0;
            if (flag9) {
                //摘要
                JSONObject FEntityJSON = new JSONObject();
                String fexplanation = lcbh+"支付" + comInfo.getLastname(sqr) + "报销差旅费差旅费";
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "2241.03");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //金蝶人员编码
                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put("FNumber", jdrybm);
                FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                FEntityJSON.put("FDetailID", FDetailID);
                //借方金额
                FEntityJSON.put("FDEBIT", xxje);
                FEntity.add(FEntityJSON);
            }
            /*单据体第十行*/
            boolean flag10 = bccxje > 0;
            if (flag10) {
                //摘要
                JSONObject FEntityJSON = new JSONObject();
                String fexplanation = lcbh+"冲抵" + comInfo.getLastname(sqr) + "借支备用金";
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "1221.03");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //金蝶人员编码
                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put("FNumber", jdrybm);
                FDetailID.put("FDETAILID__FFLEX7", FDETAILID__FFLEX7);
                FEntityJSON.put("FDetailID", FDetailID);
                //贷方金额
                FEntityJSON.put("FCREDIT", bccxje);
                FEntity.add(FEntityJSON);
            }
            /*单据体第六行*/
            boolean flag11 = yxzfje > 0;
            if (flag11) {
                //摘要
                JSONObject FEntityJSON = new JSONObject();
                String fexplanation = lcbh+"支付" + comInfo.getLastname(sqr) + "报销费用";
                FEntityJSON.put("FEXPLANATION", fexplanation);
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put("FNumber", "1002.01");
                FEntityJSON.put("FACCOUNTID", FACCOUNTID);
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //银行账号
                JSONObject FDETAILID__FF100006 = new JSONObject();
                FDETAILID__FF100006.put("FNumber", zfzhdxwb);
                FDetailID.put("FDETAILID__FF100006", FDETAILID__FF100006);
                FEntityJSON.put("FDetailID", FDetailID);
                //贷方金额
                FEntityJSON.put("FCREDIT", yxzfje);
                FEntity.add(FEntityJSON);
            }
            dataJson.put("zfrq", zfrq);
            dataJson.put("FEntity", FEntity);
            writeLog(">>>>>WeaClfbxlcBBAction>>>>>dataJson=" + dataJson.toJSONString());
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaClfbxlcBBAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("bookid", bookid);
            dataMap.put("cz", src);
            JSONObject resultJson = KingdeeUtil.passCertificate(dataJson, dataMap);
            writeLog(">>>>>WeaClfbxlcBBAction>>>>>resultJson=" + resultJson.toJSONString());
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String dataStr = Util.null2String(resultJson.getString("data"));
                JSONObject retrunDataJson = JSONObject.parseObject(dataStr);
                JSONObject ResultJson = JSONObject.parseObject(retrunDataJson.getString("Result"));
                JSONObject ResponseStatusJson = JSONObject.parseObject(ResultJson.getString("ResponseStatus"));
                String IsSuccess = Util.null2String(ResponseStatusJson.getString("IsSuccess"));
                if ("false".equals(IsSuccess)) {
                    message += Util.null2String(ResponseStatusJson.getString("Errors"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}